package homework01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print("<h1>这里是结账处,您的姓名以及选择的商品是：<br />");
        HttpSession session = request.getSession();
        String personName = (String) session.getAttribute("customerName");
        out.println("<br><h2>您的姓名：" + personName);
        out.println("<br><h2>购物车中的商品：<br>");
        Product[] products = (Product[]) session.getAttribute("products");
        if (products != null) {
            for (Product product : products) {
                Product cartItem = (Product) session.getAttribute(product.getName());
                if (cartItem != null) {
                    out.println(product.getName() + "<h3>- 价格：" + product.getPrice() + "<h3> - 库存：" + cartItem.getStock() + "<br>");
                }
            }
        }

        out.println("<P><h4>点击超链接，链接到食品柜的页面，购买食品。");
        out.println("<h4><A href='food.html'>  欢迎去选购！</A>");
        out.println("<h4><P>点击超链接，链接到login.html的页面,去修改姓名。");
        out.println("<h4><A href='login.html'>  欢迎去用户登录页面！</A>");
        out.println("<h4><P>点击超链接，链接到登录页面,退出");
        out.println("<h4><A href='/homework02_war_exploded/LogOutServlet'> 退出！</A>");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
